import sys
import os
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class picture(QWidget):
    def __init__(self):
        super(picture, self).__init__()

        self.resize(1000, 600)
        self.setWindowTitle("label显示图片")

        self.label = QLabel(self)
        self.label.setText("   显示图片")
        self.label.setFixedSize(300, 200)
        self.label.move(5, 50)

        self.label.setStyleSheet(
            "QLabel{background:white;}"
            "QLabel{color:rgb(300,300,300,120);font-size:10px;font-weight:bold;font-family:宋体;}")

        btn = QPushButton(self)
        btn.setText("打开图片")
        btn.move(10, 30)
        btn.clicked.connect(self.openimage)

    def openimage(self):
        print(os.path.abspath(os.path.dirname(__file__)))
        imgName, imgType = QFileDialog.getOpenFileName(
            self, "打开图片", "", "*.png;;*.jpg;;All Files(*)")
        # jpg = QtGui.QPixmap(imgName).scaled(self.label.width(), self.label.height())
        print(imgName)
        print(
            'size:',
            QtGui.QPixmap(imgName).height(),
            QtGui.QPixmap(imgName).width())
        jpg = QtGui.QPixmap(imgName).scaled(
            self.label.width(), self.label.height())
        self.label.setPixmap(jpg)
        # self.label.setPixmap(QPixmap('C:\\Users\\Administrator\\Pictures\\拼音笔顺.jpg'))
        # # png = QtGui.QPixmap(imgName).scaled(self.label.width(), self.label.height())
        # # self.label.setPixmap(png)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    my = picture()
    my.show()
    sys.exit(app.exec_())
